#include <ctime>

#include "XM_Timestamp.hh"

XM_NAMESPACE_BEGIN

XM_Timestamp::XM_Timestamp() : microSecSinceEpoch__(0)
{}

XM_Timestamp::XM_Timestamp(int64_t microSecSinceEpoch)
: microSecSinceEpoch__(microSecSinceEpoch)
{}

XM_Timestamp XM_Timestamp::now()
{
    return XM_Timestamp(time(NULL));
}

#define THIS_TIMESTAMP_BUF_SIZE 128

std::string XM_Timestamp::toString() const
{
    char buf[THIS_TIMESTAMP_BUF_SIZE] = {0};
    tm* tm_time = localtime(&microSecSinceEpoch__);

    snprintf(buf, THIS_TIMESTAMP_BUF_SIZE, 
        "%4d/%02d/%02d %02d:%02d:%02d",
        tm_time->tm_year + 1990,
        tm_time->tm_mon + 1,
        tm_time->tm_mday,
        tm_time->tm_hour,
        tm_time->tm_min,
        tm_time->tm_sec
    );

    return buf;
}

XM_NAMESPACE_END